/**
 * Copyright &copy; 2018-2018 <a href="#">聚成科技</a> All rights reserved.
 */
package com.jckj.core.controller;

import com.github.pagehelper.PageInfo;
import com.jckj.core.annotation.LogTitle;
import com.jckj.core.contants.SysContants;
import com.jckj.core.entity.FarmPark;
import com.jckj.core.enums.FarmParkStatus;
import com.jckj.core.service.ParkService;
import com.jckj.core.util.ClassUtils;
import com.jckj.core.util.StringUtils;
import com.jckj.core.validator.groups.Edit;
import com.jckj.core.vos.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Calendar;

/**
 * <p>
 *    园区controller
 * </p>
 *
 * @author 刘志远
 * @date 18-2-5
 */
@RequestMapping("/admin")
@Controller
public class FarmParkController {

    private static Logger LOGGER = LoggerFactory.getLogger(FarmParkController.class);

    @Autowired
    LoginUserVo loginUserVo;

    @Autowired
    ParkService parkService;


    @RequestMapping("/farmParkList")
    public ModelAndView farmParkList(){
        ModelAndView modelAndView = new ModelAndView("farmParkList");
        modelAndView.addObject("status", FarmParkStatus.values());
        return modelAndView;
    }

    @RequestMapping("/addFarmPark")
    public String addFarmPark(){
        return "addFarmPark";
    }

    @RequestMapping(value = "/saveFarmPark", method = RequestMethod.POST)
    @ResponseBody
    @LogTitle(title = "增加园区")
    public Result saveFarmPark(@ModelAttribute @Validated FarmParkVo farmParkVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        FarmPark farmPark = new FarmPark();
        try {
            ClassUtils.copyValue(farmParkVo, farmPark);
        } catch (Exception e) {
            e.printStackTrace();
        }
        farmPark.setId(StringUtils.genId());
        farmPark.setCreateBy(loginUserVo.getId());
        farmPark.setCreateTime(Calendar.getInstance().getTime());
        farmPark.setStatus(FarmParkStatus.NORMAL.getCode());
        farmPark.setLevel((byte)0);
        parkService.add(farmPark);
        return Result.ok();
    }

    @RequestMapping("/checkFarmpark")
    @ResponseBody
    public Boolean checkFarmpark(FarmParkQueryVo queryFarmParkVo){
        long count = parkService.checkParkNo(queryFarmParkVo);
        if(count==0){
            return true;
        }else{
            return false;
        }
    }

    /**
     * 查询人员列表
     * @param farmParkQueryVo
     * @return
     */
    @RequestMapping(value = "getFarmParkList", method = RequestMethod.GET)
    @ResponseBody
    public LayPageVo getFarmParkList(FarmParkQueryVo farmParkQueryVo){
        if(farmParkQueryVo.getLimit()==null||farmParkQueryVo.getLimit()==0){
            farmParkQueryVo.setLimit(SysContants.LIMIT);
        }
        if(farmParkQueryVo.getPage()==null||farmParkQueryVo.getPage()==0){
            farmParkQueryVo.setPage(1);
        }
        PageInfo<FarmPark> pageInfo = parkService.findByPage(farmParkQueryVo);
        LayPageVo layPageVo = new LayPageVo();
        layPageVo.setCode(0);
        layPageVo.setCount(pageInfo.getTotal());
        layPageVo.setData(pageInfo.getList());
        return layPageVo;
    }

    @RequestMapping(value = "farmParkView", method = RequestMethod.GET)
    public ModelAndView farmParkView(@RequestParam(required = true, name = "id") String id){
        FarmPark farmPark = parkService.querySingleFramParkById(id);
        if(farmPark==null){
            ModelAndView modelAndView = new ModelAndView("404");
            return modelAndView;
        }
        ModelAndView modelAndView = new ModelAndView("farmParkView");
        modelAndView.addObject("farmPark", farmPark);
        return modelAndView;
    }

    /**
     * 中转到编辑页面
     * @param id
     * @return
     */
    @RequestMapping(value = "editFarmPack", method = RequestMethod.GET)
    public ModelAndView editFarmPack(@RequestParam(required = true, name = "id") String id){
        FarmPark farmPark = parkService.querySingleFramParkById(id);
        if(farmPark==null){
            ModelAndView modelAndView = new ModelAndView("404");
            return modelAndView;
        }
        ModelAndView modelAndView = new ModelAndView("editFarmPack");
        modelAndView.addObject("farmPark", farmPark);
        return modelAndView;
    }

    /**
     * 更新园区信息
     * @param farmParkVo
     * @param bindingResult
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "updateFarmPark", method = RequestMethod.POST)
    @LogTitle(title = "更新园区信息")
    public Result updateFarmPark(@ModelAttribute @Validated(value = {Edit.class}) FarmParkVo farmParkVo, BindingResult bindingResult){
        if(bindingResult.hasErrors()){
            return Result.invalidParams(bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        FarmPark farmPark  = new FarmPark();
        try {
            ClassUtils.copyValue(farmParkVo, farmPark);
        } catch (Exception e) {
            LOGGER.error("FarmParkController-updateFarmPark", e.fillInStackTrace());
            e.printStackTrace();
        }
        farmPark.setUpdateBy(loginUserVo.getId());
        farmPark.setUpdateTime(Calendar.getInstance().getTime());
        parkService.update(farmPark);
        return Result.ok();
    }

    /**
     *　禁用园区
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "disableFarmPark", method = RequestMethod.POST)
    @LogTitle(title = "禁用园区")
    public Result disableFarmPark(@RequestParam(required = true, name = "id") String id){
        FarmPark farmPark = new FarmPark();
        farmPark.setId(id);
        farmPark.setUpdateBy(loginUserVo.getId());
        farmPark.setUpdateTime(Calendar.getInstance().getTime());
        farmPark.setStatus(FarmParkStatus.LOCKED.getCode());
        parkService.disabledFarmPark(farmPark);
        return Result.ok();
    }

    /**
     *　启用园区
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "enableFarmPark", method = RequestMethod.POST)
    @LogTitle(title = "启用园区")
    public Result enableFarmPark(@RequestParam(required = true, name = "id") String id){
        FarmPark farmPark = new FarmPark();
        farmPark.setId(id);
        farmPark.setUpdateBy(loginUserVo.getId());
        farmPark.setUpdateTime(Calendar.getInstance().getTime());
        farmPark.setStatus(FarmParkStatus.NORMAL.getCode());
        parkService.enabledFarmPark(farmPark);
        return Result.ok();
    }

    /**
     *　删除园区
     * @param id
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "deleteFarmPark", method = RequestMethod.POST)
    @LogTitle(title = "删除园区")
    public Result deleteFarmPark(@RequestParam(required = true, name = "id") String id){
        FarmPark farmPark = new FarmPark();
        farmPark.setId(id);
        farmPark.setUpdateBy(loginUserVo.getId());
        farmPark.setUpdateTime(Calendar.getInstance().getTime());
        farmPark.setStatus(FarmParkStatus.DELETED.getCode());
        parkService.delete(farmPark);
        return Result.ok();
    }
}